/* Copyright (c) 2021, National University of Defense Technology. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "csr_matmultivec.h"

namespace YHAMG
{

void CSR_MatMultiVec(const CSR_Matrix& A, const MultiVector& X, const MultiVector& Y)
{
	int n = A.size[0];
	int m = X.num_vectors;
	int nx = X.size;
	int ny = Y.size;
	int* Ap = A.rowptr;
	int* Ai = A.colind;
	double* Av = A.values;
	double* Xv = X.values;
	double* Yv = Y.values;

#ifdef USE_OPENMP
#pragma omp parallel for schedule(guided)
#endif
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			double temp = 0.0;
			for (int k = Ap[i]; k < Ap[i + 1]; ++k)
				temp += Av[k] * Xv[Ai[k] + j * nx];
			Yv[i + j * ny] = temp;
		}
	}
}

}